﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Frame;
using Shell32;

namespace ApplicationManagement
{
    public partial class Add : Form
    {
        private Global m_Global;
        
        public Global global
        {
            set { m_Global = value; }
            get { return m_Global; }
        }

        private AppClass appClass;


        public Add(AppClass appClass)
        {
            InitializeComponent();
            this.appClass = appClass;
            this.AllowDrop = true;
            this.TopMost = true;
            ScreenHelper.ShowScreen(this);
        }

        public Add()
        {
            InitializeComponent();
            this.appClass = null;
            this.AllowDrop = true;
            this.TopMost = true;
            ScreenHelper.ShowScreen(this);
        }

        private void btnPath_Click(object sender, EventArgs e)
        {
            OpenFileDialog of = new OpenFileDialog();
            if(IsApp())
            {
                of.DefaultExt = "exe;lnk";
                of.Filter = "应用程序 (*.exe;*lnk)|*.exe;*lnk";               
                this.Text = "添加应用";
            }
            else
            {
                of.DefaultExt = "png;ico";
                of.Filter = "图形(*.ico, *.png)|*.ico;*.png";
                of.InitialDirectory = System.Windows.Forms.Application.StartupPath;

            }
            if (of.ShowDialog() == DialogResult.OK)
            {
                if(IsApp())
                {
                    if (System.IO.Path.GetExtension(of.FileName).ToLower() == ".lnk")
                    {
                        tbxPath.Text = DirectoryHelper.GetShortCutTarget(of.FileName);                       
                        tbxName.Text = System.IO.Path.GetFileNameWithoutExtension(tbxPath.Text);
                        return;
                    }
                    else
                    {
                        tbxName.Text = System.IO.Path.GetFileNameWithoutExtension(of.FileName);
                        tbxPath.Text = of.FileName;
                    }
                    pictureBox1.Image = Icon.ExtractAssociatedIcon(tbxPath.Text).ToBitmap();
                }
                else
                {
                    tbxPath.Text = of.FileName;
                    pictureBox1.Image = Icon.ExtractAssociatedIcon(tbxPath.Text).ToBitmap();
                }
                
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (tbxName.Text == string.Empty || tbxPath.Text == string.Empty) return;
            if(IsApp())
            {
                App app = new App(tbxName.Text, tbxPath.Text);
                appClass.AppList.Add(app);               
            }
            else
            {
                appClass = new AppClass(tbxName.Text);
                appClass.image = m_Global.get_file_name(tbxPath.Text);
                m_Global.App.ApplicationManager.Application.Add(appClass);
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void tbnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private bool IsApp()
        {
            return appClass != null;
        }

        private void Add_Load(object sender, EventArgs e)
        {
            if(IsApp())
            {
                this.Text = "添加应用" ;
                lPath.Text = "路径";
            }
            else
            {
                this.Text = "添加分类";
                lPath.Text = "图标";
            }
            this.Icon = Properties.Resources.添加;
        }

        private void Add_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
                foreach (string file in files)
                {
                    if (System.IO.Path.GetExtension(file).ToLower() == ".lnk" || System.IO.Path.GetExtension(file).ToLower() == ".exe")
                    {
                        e.Effect = DragDropEffects.Copy;
                        return;
                    }
                }
            }

            e.Effect = DragDropEffects.None;
        }

        private void Add_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
                foreach (string file in files)
                {
                    if (System.IO.Path.GetExtension(file).ToLower() == ".lnk")
                    {
                        tbxPath.Text = DirectoryHelper.GetShortCutTarget(file);
                        pictureBox1.Image = Icon.ExtractAssociatedIcon(tbxPath.Text).ToBitmap();
                        tbxName.Text = System.IO.Path.GetFileNameWithoutExtension(tbxPath.Text);
                        return;
                    }
                    else if (System.IO.Path.GetExtension(file).ToLower() == ".exe")
                    {
                        tbxPath.Text = file;
                        pictureBox1.Image = Icon.ExtractAssociatedIcon(tbxPath.Text).ToBitmap();
                        tbxName.Text = System.IO.Path.GetFileNameWithoutExtension(tbxPath.Text);
                    }
                }
            }
        }
    }
}
